<?php
// 修复admin用户权限
$host = '127.0.0.1';
$port = 3306;
$database = 'qiandao';
$username = 'root';
$password = 'root';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "开始修复admin用户权限...\n\n";
    
    // 获取所有权限ID
    $stmt = $pdo->query("SELECT id FROM system_auth WHERE status = 1");
    $auths = $stmt->fetchAll(PDO::FETCH_COLUMN);
    
    if (count($auths) > 0) {
        $authorize = implode(',', $auths);
        echo "找到 " . count($auths) . " 个权限: {$authorize}\n\n";
        
        // 更新admin用户
        $stmt = $pdo->prepare("UPDATE system_user SET authorize = ? WHERE username = 'admin'");
        $stmt->execute([$authorize]);
        
        echo "✓ admin用户权限已更新\n";
    } else {
        echo "✗ 没有找到任何权限，需要先创建权限\n";
        echo "提示：admin是超级管理员，不需要配置权限也能访问所有功能\n";
    }
    
    // 验证更新
    $stmt = $pdo->prepare("SELECT * FROM system_user WHERE username = ?");
    $stmt->execute(['admin']);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
    
    echo "\n当前admin用户信息:\n";
    echo "用户ID: {$user['id']}\n";
    echo "用户名: {$user['username']}\n";
    echo "授权: {$user['authorize']}\n";
    echo "状态: {$user['status']}\n";
    
} catch (PDOException $e) {
    echo "错误: " . $e->getMessage() . "\n";
}

